package com.yunrui.device.controller;

import com.yunrui.device.common.Result;
import com.yunrui.device.entity.DeviceMonitor;
import com.yunrui.device.service.DeviceMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "设备监控规则接口")
@RestController
@RequestMapping("/monitor")
public class DeviceMonitorController {

    @Autowired
    private DeviceMonitorService deviceMonitorService;

    @ApiOperation("获取监控规则列表")
    @GetMapping("/list")
    public Result<List<DeviceMonitor>> list() {
        List<DeviceMonitor> list = deviceMonitorService.list();
        return Result.success(list);
    }

    @ApiOperation("添加监控规则")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody DeviceMonitor monitor) {
        boolean success = deviceMonitorService.save(monitor);
        return success ? Result.success(true) : Result.error("添加失败");
    }

    @ApiOperation("更新监控规则")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody DeviceMonitor monitor) {
        boolean success = deviceMonitorService.updateById(monitor);
        return success ? Result.success(true) : Result.error("更新失败");
    }

    @ApiOperation("删除监控规则")
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@ApiParam("规则ID") @PathVariable Long id) {
        boolean success = deviceMonitorService.removeById(id);
        return success ? Result.success(true) : Result.error("删除失败");
    }
}